package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.anno.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;


	@Operation(summary = "上传声音")
	@PostMapping(value = "uploadTrack")
	public Result<UploadTrackVo> uploadTrack(MultipartFile file){
		UploadTrackVo uploadTrackVo = trackInfoService.uploadTrack(file);
		return Result.ok(uploadTrackVo);
	}

	//http://127.0.0.1:4523/m1/4316086-0-default/api/album/trackInfo/saveTrackInfo
	@Operation(summary = "新增声音")
	@PostMapping(value = "saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	///api/album/trackInfo/findUserTrackPage/{page}/{limit}
	@Operation(summary = "查询当前用户声音分页列表")
	@PostMapping(value = "/findUserTrackPage/{Page}/{limit}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@RequestBody TrackInfoQuery trackInfoQuery,
													   @PathVariable(value = "Page") Integer PageNo,
													   @PathVariable(value = "limit") Integer PageSize){
		IPage<TrackListVo> pageModel = trackInfoService.findUserTrackPage(trackInfoQuery, PageNo, PageSize);
		return Result.ok(pageModel);
	}

	///api/album/trackInfo/updateTrackInfo/{id}
	@Operation(summary = "修改声音-根据id查询声音-回显")
	@GetMapping (value = "getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable(value = "id")Long id){
		//MybatisPlus提供的方法 根据id查询数据
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	//http://127.0.0.1:4523/m1/4316086-0-default/api/album/trackInfo/updateTrackInfo/1
	@Operation(summary = "修改声音-根据id查询声音-保存修改数据")
	@PutMapping(value = "updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable(value = "id") Integer id,
								  @RequestBody TrackInfoVo trackInfoVo){
       trackInfoService.updateTrackInfo(id,trackInfoVo);
	   return Result.ok();
	}

	///api/album/trackInfo/removeTrackInfo/{id}
	@Operation(summary = "删除声音")
	@DeleteMapping(value = "removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable(value = "id") Long trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	@Tag(name = "专辑详情页面下面的声音菜单")
	@GuiguLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@Parameter(name = "albumId", description = "专辑id", required = true) @PathVariable Long albumId,
			@Parameter(name = "page", description = "当前页码", required = true) @PathVariable(name = "page") Long pageNo,
			@Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable(name = "limit") Long pageSize) {
		IPage<AlbumTrackListVo> page = trackInfoService.findAlbumTrackPage(albumId , pageNo , pageSize);
		return Result.ok(page);
	}

	///api/album/trackInfo/getTrackStatVo/{trackId}
	@Operation(summary = "获取声音统计信息")
	@GetMapping("getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable(value = "trackId") Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}


	@GuiguLogin
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping(value = "/findUserTrackPaidList/{trackId}")
	public Result<List<TrackPayVo>> findUserTrackPaidList(@PathVariable(name = "trackId")Long trackId){
		List<TrackPayVo> trackListVoList = trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(trackListVoList);
	}


	// 获取用户要购买的声音的列表
	@Operation(summary = "获取用户要购买的声音的列表-Feign")
	@GetMapping(value = "/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable(value = "trackId") Long trackId , @PathVariable(value = "trackCount") Integer trackCount) {
		List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(trackId, trackCount);
		return Result.ok(list);
	}
}

